package com.itcncp.common.util;
import com.itcncp.common.model.XianxingModels;
import com.itcncp.common.rongCloud.models.XianxingHttpResult;
import com.itcncp.common.rongCloud.util.HttpUtil;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.util.HashMap;
import java.util.Map;

public class XianxingUtils {
	
	private static final String url_xianxing = "http://v.juhe.cn/xianxing/index";
	private static final String key_xianxing = "377eda13eb71acd9a9271b7ac5beac37";


	private static final String url_weather = "http://jisutianqi.market.alicloudapi.com";
	private static final String key_weather = "APPCODE 7de7f3f5bde548db95b908d4cb4dc054";


	/**
	 * 城市限行信息
	 * @param city 城市拼音代码
	 * @return
	 */
	public static XianxingModels getXianxingCity(String city)  {

		HttpURLConnection conn;

		try {
			conn = HttpUtil.CreatePostHttpConnection("", "", url_xianxing );

			StringBuilder sb = new StringBuilder();
			sb.append("dtype=json");
			sb.append("&city=").append(city);
			sb.append("&type=1");
			sb.append("&key=").append(key_xianxing);

			HttpUtil.setBodyParameter(sb, conn);

			XianxingHttpResult XianxingHttpResult =  HttpUtil.returnXianxingResult(conn);
			XianxingModels xianxingModels = JsonUtils.toBean(XianxingHttpResult.getResult(), XianxingModels.class);

			return xianxingModels;

		} catch (MalformedURLException e) {
			return new XianxingModels(-1,null,e.getMessage());
		} catch (ProtocolException e) {
			return new XianxingModels(-1,null, e.getMessage());
		} catch (IOException e) {
			return new XianxingModels(-1,null, e.getMessage());
		}catch (Exception e) {
			return new XianxingModels(-1,null, e.getMessage());
		}
	}

	/**
	 * 城市信息
	 * @param city 城市名称
	 * @return
	 */
	public static String getWeather(String city)  {

		HashMap<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization",key_weather);


		HashMap<String, String> querys = new HashMap<String, String>();
		querys.put("city",city);

		HttpResponse verifiResponse = null;
		try {
			verifiResponse = HttpUtils.doGet(url_weather,
                    "/weather/query", "GET",
                    headers, querys);

			return  EntityUtils.toString(verifiResponse.getEntity());
		} catch (Exception e) {
			return "";
		}
	}


	public static void main(String[] args) {
		String msg = getWeather("北京");

		System.out.println("天气信息获取"+msg.length()+"============"+msg);

	}
}
